<?php
session_start();
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/ArticleService.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/CustomerService.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Entity/Domain/OrderDetailsDomain.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Entity/Info/OrderDetail.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Context/BaseContext.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/ServiceResult/ServiceResult.php';
if(isset($_GET['VAT']))
{		
		$context = new BaseContext();
		$result = new ServiceResult();
		$customerServices = new CustomerServices();
		$customerID = $_GET['customerID'];
		$customer = $customerServices->getCustomerByOid($context, $result, $customerID);
		$commTax = $customer->getCustomerDomain()->getComm();
			
		$vat = $_GET['VAT'];
		
		$_SESSION['VAT'] = $vat;
		
		$res = array();
		$str = '';
		$totalAmount = 0;
		$totalBartTax = 0;
		$totalCbm = 0;
		$totalQuantity = 0;		
		$strCurrency = "";
		$count = 1;
		$totalAmountFact = 0;
		if(isset($_SESSION['orderDetail']))
		{
			$orderDetailArr = $_SESSION['orderDetail'];
			foreach ($orderDetailArr as $orderDetail)
		    {
		    	$orderDetail = unserialize($orderDetail);
		    	$strCurrency = $orderDetail->getOrderdetailsDomain()->getCurrency();
		    	
		    	$totalAmount = $totalAmount + $orderDetail->getOrderdetailsDomain()->getAmount();
		    	$totalCbm = $totalCbm + ($orderDetail->getArticle()->getArticleDomain()->getCbm() * $orderDetail->getOrderdetailsDomain()->getQuantity());
		    	$totalAmountFact = $totalAmountFact + $orderDetail->getOrderdetailsDomain()->getAmountFact();
		    	$count++;
		    }
		}
		$totalBartTax = $totalAmount * ($commTax/100);
		$totalAmountBeforeTax = $totalAmount + $totalBartTax;
		
		$totalQuantity = $totalQuantity + $orderDetail->getOrderdetailsDomain()->getQuantity();
		$totalAmountAfterTax = $totalAmountBeforeTax + ($totalAmountBeforeTax * ($vat/100));
	    $totalAmountAfterTaxFact = $totalAmountFact + ($totalAmountFact * ($vat/100));
		$strTfoot = '<tr class="gradeX odd2">
							<td colspan="7"></td>
							<td align="right" style="border-left: 1px solid #bbb;" >ttl cbm</td>
							<td id="totalCbm" align="right" style="border-right: 1px solid #bbb;">'.$totalCbm.'</td>
							<td align="right" style="border-right: 1px solid #bbb;">'.$totalQuantity.'</td>
							<td align="right"></td>
							<td align=center></td>
							<td align=right style="border: 1px solid #bbb;"></td>
							<td></td>
						</tr>';
	    if($commTax > 0)
	    {
		    $strTfoot .= '		<tr class="gradeX odd" style="height: 100px; border-top: 2px dotted #333 ;">
									<td colspan="9" style="vertical-align: middle;">comm commission B-Art '.$commTax.'%</td>                                 
									<td align="right" style="vertical-align: middle;">
			                                    	'.($commTax/100).'
									</td>
									<td align="right" style="vertical-align: middle;">
										'.$totalAmount.'
									</td>
			                                    <td align="center" style="vertical-align: middle;">
				                                    '.
			    										$strCurrency
				                                    .'
			                                    </td>
			                                    <td align="right" style="vertical-align: middle;">
													'.$totalBartTax.'
			                                    </td>
			                                    <td></td>
                                </tr>';
	    }
	    $strTfoot .= '           <tr class="gradeX odd1">
	                                	<td colspan="10">Amount Chargeable</td>
	                                	<td align="right">Total -</td>                            
	                                	<td align="center">
	                                		'.
	    										$strCurrency
		                                    .'
		                                </td>
	                                	<td class="total" align=right>
	                                		'.$totalAmountBeforeTax.'
	                                    </td>
	                                </tr>
	                                 <tr class="gradeX odd1">
	                                	<td colspan="9"></td>
	                                	<td>VAT</td>
	                                	<td style="text-align:right;">'. $vat .'%</td>                                	
	                                	<td>-</td>
	                                	<td align="right" style="border:1px solid #bbb ;">'.$totalAmountBeforeTax * ($vat/100).'</td>
	                                </tr>
	                                 <tr class="gradeX odd2">
	                                	<td colspan="10"></td>
	                                	<td align="right">Total</td>
	                                	<td align=center>	
	                                		'.
	    										$strCurrency
		                                    .'
	                                	</td>
	                                	<td align=right style="border:1px solid #bbb ;">
	                                		'.$totalAmountAfterTax.'
	                                    </td>
	                                </tr>	                                                 
	                                <input type="hidden" name="sum'.$strCurrency.'" value="'.$totalAmountAfterTax.'" >
	                                <input type="hidden" name="sum'.$strCurrency.'Fact" value="'.$totalAmountAfterTaxFact.'" > 
						';
		$res['tfoot'] = $strTfoot;
	    echo json_encode($res);	
}
?>